package com.lxq.LeetCode.lengthOfLongestSubstring;

import java.util.HashMap;

public class Solution {
    public static void main(String[] args) {
        int res = lengthOfLongestSubstring("abba");
        System.out.println(res);
    }

    public static int lengthOfLongestSubstring(String s) {

        int result = 0;
        int num = 1;
        int removeValue = 0;
        HashMap<Character, Integer> alphabet = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char temp = s.charAt(i);
            if (alphabet.containsKey(temp)) {
                if (result < (num - 1 - removeValue)) {
                    result = (num - 1 - removeValue);
                }
                if (removeValue < alphabet.get(temp)) {
                    removeValue = alphabet.get(temp);
                }
                alphabet.put(temp, num);
                num++;
            } else {
                alphabet.put(temp, num);
                num++;
            }
        }
        if (result < (num - 1 - removeValue)) {
            result = (num - 1 - removeValue);
        }
        return result;
    }
}
